<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="系统类型" prop="systemType">
        <el-input
          v-model="queryParams.systemType"
          placeholder="请输入服务器系统类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ip地址" prop="hostIp">
        <el-input
          v-model="queryParams.hostIp"
          placeholder="请输入服务器ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ping状态" prop="isPing">
        <!-- <el-input
          v-model="queryParams.isPing"
          placeholder="请输入 0不通 1通"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.isPing" @keyup.enter.native="handleQuery" clearable placeholder="请选择">
          <el-option
            v-for="item in [{value: 1, label: '正常'}, {value: 0, label: '异常'}]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 40px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['serve:serveInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['serve:serveInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['serve:serveInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['serve:serveInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" stripe border :data="serveInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" width="70" align="center" prop="serverId" /> -->
      <el-table-column label="系统类型" width="100" align="center" prop="systemType" />
      <el-table-column label="ip地址" width="120" align="center" prop="hostIp" />
      <el-table-column label="总内存" align="center" prop="totalMemory" >
        <template slot-scope="scope">
          <span >{{ scope.row.totalMemory }}MB</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="已使用内存" width="100" align="center" prop="useMemory">
        <template slot-scope="scope">
          <span >{{ scope.row.useMemory }}MB</span>
        </template>
      </el-table-column> -->

      <el-table-column label="总磁盘" align="center" prop="totalDisk" />

      <el-table-column label="%内存" width="80" align="center" prop="useMemory">
        <template slot-scope="scope">
          <!-- <span >{{ parseInt(scope.row.useMemory*10000/scope.row.totalMemory)/100}}%</span> -->
          <BallChart width="60px" height="60px" :baifei="scope.row.useMemory/scope.row.totalMemory" :tooltip="`已使用: ${scope.row.useMemory}`" />
        </template>
      </el-table-column>

      <!-- <el-table-column label="使用磁盘" align="center" prop="useDisk" /> -->
      <el-table-column label="%磁盘" width="80" align="center" prop="useDisk" >
        <template slot-scope="scope">
          <BallChart width="60px" height="60px" :baifei="scope.row.useDisk.slice(0,-1)/scope.row.totalDisk.slice(0,-1)" :tooltip="`已使用: ${scope.row.useDisk}`" />
        </template>
      </el-table-column>
      <el-table-column label="%CPU" width="80" align="center" prop="cpuOccupy">
        <template slot-scope="scope">
          <BallChart width="60px" height="60px" :baifei="scope.row.cpuOccupy/100"/>
        </template>
      </el-table-column>
      <el-table-column label="ping状态" align="center" prop="isPing" >
        <template slot-scope="scope">
          <div v-if="scope.row.isPing===1" >
            <!-- <div style="width: 8px;height: 8px;border-radius: 50%;background-color: #42b983;margin: 8px 5px 0 0"></div>
            <span >连通</span> -->
            <el-tag type="success">正常</el-tag>
          </div>
          <div v-if="scope.row.isPing===0" >
            <!-- <div style="width: 8px;height: 8px;border-radius: 50%;background-color: red;margin: 8px 5px 0 0"></div>
            <span >连通</span> -->
            <el-tag type="danger">异常</el-tag>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="ping响应延迟" align="center" prop="pingDelay" />-->
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            @click="handledePort(scope.row)"
          >端口信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['serve:serveInfo:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['serve:serveInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系统类型" prop="systemType">
          <!-- <el-input v-model="form.systemType" placeholder="请输入服务器系统类型" /> -->
          <el-select v-model="form.systemType" placeholder="请输入服务器系统类型">
          <el-option
            v-for="item in [{value: 'Linux', label: 'Linux'}]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="ip地址" prop="hostIp">
          <el-input v-model="form.hostIp" placeholder="请输入服务器ip地址" />
        </el-form-item>
        <el-form-item label="ssh端口" prop="sshPort">
          <el-input v-model="form.sshPort" placeholder="请输入ssh访问端口" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入登入账号用户名" />
        </el-form-item>
        <el-form-item label="登入密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入登入密码" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 端口需求列表对话框 -->
    <el-dialog title="端口使用情况" :visible.sync="portOpen"  append-to-body>
      <el-table v-loading="loadingPorts" :data="portInfo" border>
        <el-table-column property="agreement" label="协议" width="150" align="center"></el-table-column>
        <el-table-column property="port" label="端口" width="200" align="center"></el-table-column>
        <el-table-column property="status" label="状态" align="center">
          <template slot-scope="scope">
          <div v-if="scope.row.status==1" >
            <el-tag type="success">正常</el-tag>
          </div>
          <div v-if="scope.row.status==0" >
            <el-tag type="danger">异常</el-tag>
          </div>
        </template>
        </el-table-column>
      </el-table>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>

</template>

<script>
import { listServeInfo, getServeInfo, delServeInfo, addServeInfo, updateServeInfo } from "@/api/serve/serveInfo";
import { getServer } from "@/api/monitor/server";
import BallChart from "../../dashboard/BallChart.vue";
import { getServePortInfo } from "../../../api/serve/serveInfo";

export default {
    name: "ServeInfo",
    components: {
      BallChart
    },
    data() {
        return {
            // 是否显示端口弹出层
            portOpen: false,
            // 端口信息
            portInfo: [],
            loadingPorts: false,
            // 详情表单
            detailsform: {},
            // 服务器信息
            server: [],
            // 详情控制层
            details: false,
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 服务列表表格数据
            serveInfoList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                systemType: null,
                hostIp: null,
                isPing: null,
                createBy: null,
                createTime: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                systemType: [
                    { required: true, message: "系统类型不能为空", trigger: "blur" }
                ],
                hostIp: [
                    { required: true, message: "ip地址不能为空", trigger: "blur" }
                ],
                sshPort: [
                    { required: true, message: "ssh访问端口不能为空", trigger: "blur" }
                ],
                userName: [
                    { required: true, message: "登录用户名不能为空", trigger: "blur" }
                ],
                password: [
                    { required: true, message: "用户密码不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
        this.getListdetaill();
        // this.openLoading();
    },
    methods: {
        /** 查询服务器信息 */
        getListdetaill() {
            getServer().then(response => {
                this.server = response.data;
                this.$modal.closeLoading();
            });
        },
        // 打开加载层
        openLoading() {
            this.$modal.loading("正在加载服务监控数据，请稍候！");
        },
        /** 查询服务列表列表 */
        getList() {
            this.loading = true;
            listServeInfo(this.queryParams).then(response => {
                console.log(response.rows);
                this.serveInfoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getPorts(e){
          let that = this
          getServePortInfo(e).then(res => {
            console.log(res, "=========")
            that.loadingPorts = false

            let portInfo = []
            res.forEach(value => {
              portInfo.push({agreement: 'tcp',agreement: 'tcp', port: value, status: 1})
            })

            that.portInfo = portInfo
          })
        },
        // 端口使用情况
        handledePort(e){
          this.portOpen = true
          this.loadingPorts = true
          this.getPorts(e)
        },
        // 详情查看
        handledetails(e) {
            this.details = true;
            this.detailsform = e;
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                serverId: null,
                systemType: null,
                hostIp: null,
                sshPort: null,
                userName: null,
                password: null,
                totalMemory: null,
                useMemory: null,
                totalDisk: null,
                useDisk: null,
                cpuOccupy: null,
                isPing: null,
                pingDelay: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.serverId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加服务列表";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const serverId = row.serverId || this.ids;
            getServeInfo(serverId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改服务列表";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.serverId != null) {
                        updateServeInfo(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                    else {
                        addServeInfo(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const serverIds = row.serverId || this.ids;
            this.$modal.confirm('是否确认删除服务列表编号为"' + serverIds + '"的数据项？').then(function () {
                return delServeInfo(serverIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('serve/serveInfo/export', {
                ...this.queryParams
            }, `serveInfo_${new Date().getTime()}.xlsx`);
        }
    }
};
</script>
